// pages/shopping/shopping.js
import request from "../../utils/request.js"
import config from "../../utils/config.js"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      test:"111",
      num:"",
      isDelete:false,
      unitPrice:"399.00",
      total:'0',
      proDesc: [],
      id:"",
      shopDesc:[],
      goodId:"",
      proId:"",
      proNum:"",
      carListTotal:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //   this.updeteCar()
  },
  getData: function () {
    var that = this;
    request.request(config.proDesc, 'GET', { id: this.data.id }, function (res) {
      console.log(res.data.data);
      that.setData({
        proDesc: res.data.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      this.total()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getCar();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      console.log(wx.getStorageSync("token"))
     
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
    //更新购物车数据
  //增加购物车商品数量
 addNumber:function(e){
     console.log(e);
     var numCar = (e.currentTarget.dataset.number)+1;
     this.updateCar(e.currentTarget.dataset.id, e.currentTarget.dataset.goods_id, numCar, e.currentTarget.dataset.product_id)
 },
  //减少购物车商品数量
    minusNum(e){
        if (e.currentTarget.dataset.number >1) {
            var numCar = (e.currentTarget.dataset.number) -1;
            this.updateCar(e.currentTarget.dataset.id, e.currentTarget.dataset.goods_id, numCar, e.currentTarget.dataset.product_id)
        } else if (e.currentTarget.dataset.number = 1){
            wx.showToast({
              title: '商品数量不能为0！',
              icon: 'none',
              duration: 1500//持续的时间
          })
        }
        this.total();
       
    },
    // 删除购物车内对应的商品
    delete(e){
        var that = this;
        console.log(e.currentTarget.dataset, typeof ((e.currentTarget.dataset.product_id).toFixed(0)));
        // this.data.isDelete=true;
       
        request.request(config.deleteCar, 'post',
         { 
            productIds:e.currentTarget.dataset.product_id.toFixed(0)
           }, function (res) {
            console.log(res);
             that.getCar()
            
        })
        this.setData({
            // isDelete:true,
            total:0
        });

    },
    total(){//计算总价
        // console.log(this.data.total);
        this.setData({
            total: (this.data.unitPrice * this.data.num).toFixed(2)
            
        })
      
        
    },
    getCar(){
        //   请求购物车数据
        var that = this;
        wx.request({
            url: config.shoppingCar,
            header: {
                "X-Nideshop-Token": wx.getStorageSync("token")
            },
            success(res) {
                console.log(res.data.data);
                that.setData({
                    shopDesc: res.data.data.cartList,
                    carListTotal: res.data.data.cartTotal.goodsAmount
                })
                console.log(that.data.shopDesc)
            }
        })
    },
    //更新购物车数据
    updateCar(id, goodsId, num, productId){
        var that = this;
        request.request(config.updataCar, 'POST',
         {
             id: id,
             goodsId: goodsId,
             number:num,
             productId: productId
         }, function (res) {
            console.log(1111,that);
            
            that.setData({
                shopDesc: res.data.data.cartList
            })
             that.getCar();
        })
    }

})